package com.bw.user.mvvm.viewmodel;

import android.app.AlertDialog;
import android.content.DialogInterface;

import androidx.databinding.ObservableField;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.Utils;
import com.bw.greendao.dao.DaoSessionUtils;
import com.bw.greendao.dao.UserEntityDao;
import com.bw.greendao.entity.UserDaoEntity;
import com.bw.greendao.entity.UserEntity;
import com.bw.mvvm_common.BaseApplication;
import com.bw.mvvm_common.entity.BaseEntity;
import com.bw.mvvm_common.filed.UIField;
import com.bw.mvvm_common.mvvm.viewmodel.BaseViewModel;
import com.bw.mvvm_common.utils.IntentAction;
import com.bw.mvvm_common.utils.SPUtils;
import com.bw.user.mvvm.model.UserModel;
import com.bw.user.mvvm.view.activity.LoginsActivity;
import com.bw.user.mvvm.view.activity.PersonalActivity;
import com.bw.user.mvvm.view.activity.SetsActivity;
import com.bw.user.mvvm.view.activity.UserOrderActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.HashMap;
import java.util.List;

/**
 * @author KK
 * @description:
 * @date :2021/10/19 19:25
 */
public class UserViewModel extends BaseViewModel<UserModel> {
        public boolean islogin=false;
    public ObservableField<String> tv = new ObservableField<>("登录/注册");
    @Override
    public void oncreate() {
        super.oncreate();
        //判断是否登录
        List<UserEntity> userEntities = new UserDaoEntity().selectAll();
        for (UserEntity userEntity : userEntities) {
            boolean flag = userEntity.getFlag();
            if (flag){
                tv.set(userEntity.getUser_name());
            }
        }
        EventBus.getDefault().register(this);
    }

    @Subscribe(sticky = true)
    public void loginusername(String s){
        tv.set(s);

    }

    @Subscribe(sticky = true)
    public void regiserusername(String s){
        tv.set(s);
    }

    @Subscribe()
    public void exitlogin(String s){
        tv.set(s);
    }


    @Override
    public void onDestory() {
        super.onDestory();
        EventBus.getDefault().unregister(this);
    }

    @Override
    protected void result(BaseEntity baseEntity) {

    }

    public void loginclick(){
        if(tv.get().equals("登录/注册")){
            HashMap<String, Object> hashMap = new HashMap<>();
            hashMap.put(UIField.PAGECLAZZ, LoginsActivity.class);
            startPage(hashMap);
            finishPage();
        }else {
            showMsg("用户已登录");
            
        }

    }

    public void personal(){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(UIField.PAGECLAZZ, PersonalActivity.class);
        startPage(hashMap);
        finishPage();
    }

    //退出登录
    public void exits(){
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ, SetsActivity.class);
        startPage(map);
    }


    //我的订单页面
    public void userOrder(){
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put(UIField.PAGECLAZZ, UserOrderActivity.class);
        startPage(hashMap);
    }

    //代付款
    public void paymentClick(){
        IntentAction intentAction = new IntentAction();
        intentAction.ation = "payment";
        EventBus.getDefault().postSticky(intentAction);
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ,UserOrderActivity.class);
        startPage(map);
    }

    //待收货
    public void waitClick(){
        IntentAction intentAction = new IntentAction();
        intentAction.ation = "wait";
        EventBus.getDefault().postSticky(intentAction);
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ,UserOrderActivity.class);
        startPage(map);
    }

    //已完成
    public void offClick(){
        IntentAction intentAction = new IntentAction();
        intentAction.ation = "off";
        EventBus.getDefault().postSticky(intentAction);
        HashMap<String, Object> map = new HashMap<>();
        map.put(UIField.PAGECLAZZ,UserOrderActivity.class);
        startPage(map);
    }


}
